package com.unimas.dof.view;

import java.io.File;
import java.io.FileInputStream;

import com.unimas.dof.annotation.ViewRender;
import com.unimas.dof.render.BinaryRender;

@ViewRender(BinaryRender.class)
public class BinaryView extends View{

	private byte[] data;
	private String fileName;
	private String contentType;
	
	public BinaryView(byte[] data){
		this.data = data;
	}
	
	public byte[] getData() {
		return data;
	}
	public void setData(byte[] data) {
		this.data = data;
	}
	public String getFileName() {
		return fileName;
	}
	public void setFileName(String fileName) {
		this.fileName = fileName;
	}
	
	public static BinaryView loadFile(String fileName) throws Exception{
		File file = new File(fileName);
		if(file.isFile() && file.exists()){
			FileInputStream fis = new FileInputStream(file);
            int length = (int)file.length();
            byte[] data = new byte[length];
            fis.read(data);
            fis.close();
            
            BinaryView view = new BinaryView(data);
            view.setFileName(file.getName());
            
            return view;
		}else{
			throw new Exception(fileName+"is not exists!");
		}
	}

	public String getContentType() {
		return contentType;
	}

	public void setContentType(String contentType) {
		this.contentType = contentType;
	}
	
}
